<?php


namespace app\api\model;

use app\common\enum\user\balanceLog\Scene as SceneEnum;
use app\common\library\helper;
use app\common\library\wechat\WxPay;
use app\common\model\Hongbao as HongbaoModel;
use app\api\model\User as UserModel;
use app\common\service\Order as OrderService;
use app\store\model\User as StoreUserModel;
use app\store\model\user\BalanceLog as BalanceLogModel;
use app\store\model\Wxapp as WxappModel;

class Hongbao extends HongbaoModel{
    public function getLists(){
        return $this->order('create_time desc')->limit(50)->select();
    }

    /**
     * @param $user               用户数据
     * @param $msg                描述
     * @param $type               0积分 1现金红包 2现金余额
     * @param int $money          现金金额
     * @param int $points         积分
     * @return false|int
     */
    public function addHongBao($user, $msg, $type, $money = 0, $points = 0){
        //查询当前用户当天是否领取过
        $where = 'uid = '.$user['user_id'].' and creata_date = "'.date('Y-m-d').'"';
        $hbCount = db('hongbao')->where($where)->count();
        if($hbCount == 0) {
            $data = [
                'order_no' => OrderService::createOrderNo(),
                'uid' => $user['user_id'],
                'nickname' => $user['nickName'],
                'hred_url' => $user['avatarUrl'],
                'msg' => $msg,
                'type' => $type,
                'money' => $money,
                'points' => $points,
                'wxapp_id' => 10001,
                'creata_date' => date('Y-m-d'),
                'create_time' => time(),
                'update_time' => time(),
            ];
            $hongBaoModel = new HongbaoModel();
            $resule = $hongBaoModel->save($data);
            $hbId = $hongBaoModel->getLastInsID();
            if ($resule) {
                //领取成功  更新用户积分
                $describe = "群内发放";
                $this->user = UserModel::detail($user['user_id']);
                if ($type == 0) {
                    //更新用户积分
                    $res = $this->user->setIncPoints($points, $describe);
                } elseif ($type == 1) {
                    //企业付款至零钱包
                    // 生成付款订单号
                    // 付款描述
                    $desc = '恭喜您成功领取';
                    // 微信支付api：企业付款到零钱
                    $wxConfig = WxappModel::getWxappCache();
                    $WxPay = new WxPay($wxConfig);
                    // 请求付款api
                    if ($WxPay->transfers($data['order_no'], $user['open_id'], $money, $desc)) {
                        // 确认已打款
                        $udata['status'] = 1;
                        $res = db('hongbao')->where(['id' => $hbId])->update($udata);
                    }
                } else {
                    //更新用户余额
                    // 用户详情
//                    $rechargeData['balance']['mode'] = 'inc';
//                    $rechargeData['balance']['money'] = $money;
//                    $rechargeData['balance']['remark'] = $describe;
//                    $model = StoreUserModel::detail($user['user_id']);

                    if (!isset($money) || $money === '' || $money < 0) {
                        $this->error = '请输入正确的金额';
                        return false;
                    }
                    // 判断充值方式，计算最终金额
                    // 更新记录
                    // 更新账户余额
                    db('user')->where('user_id', '=' , $user['user_id'])->setInc('balance', $money);
                    $res = db('user_balance_log')->insert([
                        'user_id' => $user['user_id'],
                        'scene' => 30,
                        'money' => $money,
                        'describe' =>$describe,
                        'wxapp_id' => 10001,
                        'create_time' => time()
                    ]);
//                    $res = $model->recharge('admin', 0, $rechargeData);
                }
                if ($res) {
                    $userInfo = UserModel::detail($user['user_id']);
                    return ['status' => 200, 'msg' => '领取成功', 'type' => $type, 'money' => $money, 'points' => $points];
                } else {
                    return ['status' => 100, 'msg' => '领取失败'];
                }
            } else {
                return ['status' => 100, 'msg' => '领取失败'];
            }
        }else{
            return ['status' => 100, 'msg' => '您今天已经领取，请明天在来！'];
        }
    }
}